package com.gill.exp.common;

import com.gill.exp.common.enums.ResultCodeEnum;

import java.io.Serializable;

public class HttpResult<T> implements Serializable {

    private static final long serialVersionUID = -1L;
    private boolean success;
    private T data;
    private String code;
    private String message;

    private HttpResult(boolean success, T data, String code, String message) {
        this.success = success;
        this.data = data;
        this.code = code;
        this.message = message;
    }

    private HttpResult(boolean success, T data, ResultCodeEnum resultCodeEnum) {
        this.success = success;
        this.data = data;
        this.code = resultCodeEnum.getCode();
        this.message = resultCodeEnum.getMessage();
    }

    /**
     * 成功返回
     */
    public static <T> HttpResult<T> ok(T data) {
        return new HttpResult<>(Boolean.TRUE, data, ResultCodeEnum.SUCCESS);
    }

    /**
     * 异常返回-指定错误码
     */
    public static HttpResult fail(ResultCodeEnum resultCodeEnum) {
        return new HttpResult<>(Boolean.FALSE, null, resultCodeEnum);
    }

    /**
     * 异常返回-非指定异常
     */
    public static HttpResult fail(String code, String message) {
        return new HttpResult<>(Boolean.FALSE, null, code, message);
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}